<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : section_builder.xsl
    Created on : November 3, 2008, 8:25 PM
    Author     : keith
    Description:
        Purpose of transformation follows.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version="1.0"
            xmlns:redirect="http://xml.apache.org/xalan/redirect"
            extension-element-prefixes="redirect">
  <xsl:output method="xml"/>
  
  <xsl:param name="outdir"/>

    <!-- TODO customize transformation rules 
         syntax recommendation http://www.w3.org/TR/xslt 
    -->
  <xsl:template match="/">
<!--    <xsl:variable name="uml" select="document('../UML2DocBook/dist/umldoc.xml')"/>
    <xsl:apply-templates select="$uml/bouml/usecases" mode="list"/>
    <xsl:apply-templates select="$uml/bouml/usecasediagrams"/>
    <xsl:apply-templates select="$uml/bouml/domainmodels"/>
    <xsl:apply-templates select="$uml/bouml/classdiagrams"/>
    <xsl:apply-templates select="$uml/bouml/classdiagrams"/>
    <xsl:apply-templates select="$uml/bouml/sequencediagrams"/>-->

    <xsl:apply-templates select="/bouml/usecases" mode="list"/>
    <xsl:apply-templates select="/bouml/usecasediagrams"/>
    <xsl:apply-templates select="/bouml/domainmodels"/>
    <xsl:apply-templates select="/bouml/classdiagrams"/>
    <xsl:apply-templates select="/bouml/classdiagrams"/>
    <xsl:apply-templates select="/bouml/sequencediagrams"/>
<!--    <html>
      <head>
        <title>section_builder.xsl</title>
      </head>
      <body>
      </body>
    </html> -->
  </xsl:template>
  
  <!-- generate docbook usecase list -->
  <xsl:template match="usecases" mode="list">
<!--    <xsl:document href="" method="xml"
                  omit-xml-declaration="yes" ident="yes">-->
    <redirect:write file="{$outdir}/usecaselist.xml">
      <chapter id="ch_usecaselist">
        <title>Use Case List</title>
        <orderedlist spacing="compact">
          <xsl:for-each select="usecase">
            <xsl:sort select="name"/>
            <listitem>
              <para><xsl:value-of select="name"/></para>
            </listitem><xsl:text>
</xsl:text>
          </xsl:for-each>
        </orderedlist>
      </chapter>
    </redirect:write>
<!--    </xsl:document>-->
  </xsl:template>
  
  <!-- generate docbook usecase diagrams -->
  <xsl:template match="usecasediagrams">
    <redirect:write file="{$outdir}/usecasediagrams.xml">
      <chapter id="ch_usecasediagrams">
        <title>Use Case Diagrams</title>
        <xsl:for-each select="usecasediagram">
          <xsl:sort select="title"/>
          <section>
            <xsl:attribute name="id">
              <xsl:value-of select="concat('s_ucd_',position())"/>
            </xsl:attribute>
            <title><xsl:value-of select="title"/></title>
            <figure>
              <xsl:attribute name="id">
                <xsl:value-of select="concat('f',id)"/>
              </xsl:attribute>
              <title><xsl:value-of select="title"/></title>
              <mediaobject>
                <xsl:attribute name="id">
                  <xsl:value-of select="concat('bouml-',image)"/>
                </xsl:attribute>
                <imageobject>
                  <imagedata width="5.5in" scalefit="1"
                              align="center" valign="top">
<!--                  <imagedata width="5.5in" depth="5.5in" 
                              contentwidth="5.5in" contentdepth="5.5in" 
                              scalefit="1" align="center" valign="top">-->
                  <xsl:attribute name="fileref">
                    <xsl:value-of select="concat('bouml/',image)"/>
                  </xsl:attribute>
                  </imagedata>
                </imageobject>
              </mediaobject>
            </figure>
            <para>
              <xsl:value-of select="description"/>
            </para>
          </section><xsl:text>
</xsl:text>
        </xsl:for-each>
      </chapter>
    </redirect:write>
  </xsl:template>
  
  <!-- generate docbook usecase diagrams -->
  <xsl:template match="sequencediagrams">
    <redirect:write file="{$outdir}/sequencediagrams.xml">
      <section id="ch_sequencediagrams">
        <title>Sequence Diagrams</title>
        <xsl:for-each select="sequencediagram">
          <xsl:sort select="title"/>
          <section>
            <xsl:attribute name="id">
              <xsl:value-of select="concat('s_sd_',position())"/>
            </xsl:attribute>
            <title><xsl:value-of select="title"/></title>
            <figure>
              <xsl:attribute name="id">
                <xsl:value-of select="concat('f',id)"/>
              </xsl:attribute>
              <title><xsl:value-of select="title"/></title>
              <mediaobject>
                <xsl:attribute name="id">
                  <xsl:value-of select="concat('bouml-',image)"/>
                </xsl:attribute>
                <imageobject>
                  <imagedata width="5.5in" scalefit="1"
                              align="center" valign="top">
<!--                  <imagedata width="5.5in" depth="5.5in" 
                              contentwidth="5.5in" contentdepth="5.5in" 
                              scalefit="1" align="center" valign="top">-->
                  <xsl:attribute name="fileref">
                    <xsl:value-of select="concat('bouml/',image)"/>
                  </xsl:attribute>
                  </imagedata>
                </imageobject>
              </mediaobject>
            </figure>
            <para>
              <xsl:value-of select="description"/>
            </para>
          </section><xsl:text>
</xsl:text>
        </xsl:for-each>
      </section>
    </redirect:write>
  </xsl:template>
  
  <!-- generate docbook domain models -->
  <xsl:template match="domainmodels">
    <redirect:write file="{$outdir}/domainmodels.xml">
      <chapter id="ch_domainmodels">
        <title>Domain Models</title>
        <xsl:for-each select="domainmodel">
          <xsl:sort select="title"/>
          <section>
            <xsl:attribute name="id">
              <xsl:value-of select="concat('s_dmd_',position())"/>
            </xsl:attribute>
            <title><xsl:value-of select="title"/></title>
            <figure>
              <xsl:attribute name="id">
                <xsl:value-of select="concat('f',id)"/>
              </xsl:attribute>
              <title><xsl:value-of select="title"/></title>
              <mediaobject>
                <xsl:attribute name="id">
                  <xsl:value-of select="concat('bouml-',image)"/>
                </xsl:attribute>
                <imageobject>
                  <imagedata width="5.5in" scalefit="1"
                              align="center" valign="top">
<!--                  <imagedata width="5.5in" depth="5.5in" 
                              contentwidth="5.5in" contentdepth="5.5in" 
                              scalefit="1" align="center" valign="top">-->
                  <xsl:attribute name="fileref">
                    <xsl:value-of select="concat('bouml/',image)"/>
                  </xsl:attribute>
                  </imagedata>
                </imageobject>
              </mediaobject>
            </figure>
            <para>
              <xsl:value-of select="description"/>
            </para>
          </section><xsl:text>
</xsl:text>
        </xsl:for-each>
      </chapter>
    </redirect:write>
  </xsl:template>
  
  <!-- generate docbook class diagrams -->
  <xsl:template match="classdiagrams">
    <redirect:write file="{$outdir}/classdiagrams.xml">
      <chapter id="ch_classdiagrams">
        <title>Class Diagram</title>
        <xsl:for-each select="classdiagram">
          <xsl:sort select="title"/>
          <section>
            <xsl:attribute name="id">
              <xsl:value-of select="concat('s_cd_',position())"/>
            </xsl:attribute>
            <title><xsl:value-of select="title"/></title>
            <figure>
              <xsl:attribute name="id">
                <xsl:value-of select="concat('f',id)"/>
              </xsl:attribute>
              <title><xsl:value-of select="title"/></title>
              <mediaobject>
                <xsl:attribute name="id">
                  <xsl:value-of select="concat('bouml-',image)"/>
                </xsl:attribute>
                <imageobject>
                  <imagedata width="5.5in" scalefit="1"
                              align="center" valign="top">
<!--                  <imagedata width="5.5in" depth="5.5in" 
                              contentwidth="5.5in" contentdepth="5.5in" 
                              scalefit="1" align="center" valign="top">-->
                  <xsl:attribute name="fileref">
                    <xsl:value-of select="concat('bouml/',image)"/>
                  </xsl:attribute>
                  </imagedata>
                </imageobject>
              </mediaobject>
            </figure>
            <para>
              <xsl:value-of select="description"/>
            </para>
          </section><xsl:text>
</xsl:text>
        </xsl:for-each>
      </chapter>
    </redirect:write>
  </xsl:template>
  
  <xsl:template match="div">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="style">
      <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="p">
    <para>
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:template match="b">
    <emphasis role="bold">
      <xsl:apply-templates/>
    </emphasis>
  </xsl:template>

  <xsl:template match="i">
    <emphasis role="italics">
      <xsl:apply-templates/>
    </emphasis>
  </xsl:template>

  <xsl:template match="pre">
    <literallayout>
      <xsl:apply-templates/>
    </literallayout>
  </xsl:template>

<!--  <xsl:template match="ul">
    <itemizedlist spacing="compact">
      <xsl:apply-templates/>
    </itemizedlist>
  </xsl:template>

  <xsl:template match="ol">
    <orderedlist spacing="compact">
      <xsl:apply-templates/>
    </orderedlist>
  </xsl:template>
  <xsl:template match="li">
    <listitem>
      <para>
        <xsl:apply-templates/>
      </para>
    </listitem>
  </xsl:template>

  -->

  <xsl:template match="ul">
    <itemizedlist >
      <xsl:for-each select="li">
        <listitem>
          <xsl:apply-templates/>
        </listitem>
      </xsl:for-each>
    </itemizedlist>
  </xsl:template>

  <xsl:template match="ol">
    <informaltable frame="none" rowsep="0" colsep="0">
      <tgroup cols="3">
        <colspec column="1" colname="c1" colwidth="0.25in" />
        <colspec column="2" colname="c2" colwidth="0.5in" />
        <colspec column="3" colname="c3" colwidth="5.0in" />
        <thead>
          <row rowsep="0" colsep="0">
            <entry/>
            <entry namest="c2" nameend="c3">
              <xsl:text>Actor</xsl:text>
            </entry>
          </row>
          <row rowsep="0" colsep="0">
            <entry/>
            <entry/>
            <entry>
              <xsl:text>System</xsl:text>
            </entry>
          </row>
        </thead>
        <tbody>
          <xsl:apply-templates/>
        </tbody>
      </tgroup>
    </informaltable>
  </xsl:template>

  <xsl:template match="li">
    <xsl:variable name="flownum">
      <xsl:choose>
        <xsl:when test="@flownum">
          <xsl:value-of select="concat(@flownum, @altlabel)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat(position(), @altlabel)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <row rowsep="0" colsep="0">
      <entry/>
      <xsl:choose>
        <xsl:when test="@flowcol='A'">
          <entry namest="c2" nameend="c3">
            <xsl:value-of select="$flownum"/><xsl:text>. </xsl:text><xsl:apply-templates/>
          </entry>
        </xsl:when>
        <xsl:otherwise>
          <entry/>
          <entry>
            <xsl:value-of select="$flownum"/><xsl:text>. </xsl:text><xsl:apply-templates/>
          </entry>
        </xsl:otherwise>
      </xsl:choose>
    </row>
  </xsl:template>

  <!-- generate docbook use case text -->
  <xsl:template match="usecase">
<!--    <xsl:text><xsl:value-of select="name"/></xsl:text>
    <xsl:variable name="uc_file" select="concat('../stateofplay/usecase_',id,'.xml')"/>
    <xsl:text>
<xsl:value-of select="$uc_file"/></xsl:text>
    <redirect:write file="{$uc_file}">-->
        <section>
          <xsl:attribute name="id">
            <xsl:value-of select="concat('s_uc_',id)"/>
          </xsl:attribute>
          <title><xsl:value-of select="name"/></title>
          <informaltable >
            <tgroup cols="1">
              <tbody>
<!--                <xsl:if test="description/name">
                  <row>
                    <entry>
                      <emphasis role="bold"><xsl:text>Name:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="name"/>
                    </entry>
                  </row>
                </xsl:if>
-->
                <xsl:if test="description/scope">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Scope:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/scope"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/level">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Level:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/levl"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/pactor">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Primary Actor:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/pactor"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/stakeholder">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Stakeholders and Interests:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/stakeholder"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/precond">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Preconditions:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/precond"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/guarantee">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Success Guarantee:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:value-of select="description/guarantee"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/main">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Main Success Scenario:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/main"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/ext">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Extensions:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/ext"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/req">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Requirements:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/req"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/tech">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Technology and Data Variations List:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/tech"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/freq">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Frequency and Occurrence:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/freq"/>
                    </entry>
                  </row>
                </xsl:if>
                <xsl:if test="description/misc">
                  <row rowsep="0">
                    <entry>
                      <emphasis role="bold"><xsl:text>Miscellaneous:</xsl:text></emphasis>
                      <xsl:text> </xsl:text>
                      <xsl:apply-templates select="description/misc"/>
                    </entry>
                  </row>
                </xsl:if>
              </tbody>
            </tgroup>
          </informaltable>
        </section><xsl:text>
</xsl:text>
<!--    </redirect:write> -->
  </xsl:template>
  
  <!-- generate docbook use case text -->
  <xsl:template match="usecases">
    <redirect:write file="{$outdir}/usecases.xml">
      <xsl:for-each select="usecase">
        <xsl:sort select="name"/>
        <xsl:if test="name='Schedule User Action' or name='Play the Game' or name='Install GameWorld'">
          <xsl:apply-templates  select="."/>
        </xsl:if>
      </xsl:for-each>
    </redirect:write>
  </xsl:template>
</xsl:stylesheet>
